package com.spx;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;


public class LGraphics implements LTrans{
	private Canvas canvas=new Canvas();
	private Paint paint=new Paint();
	private int width,height;
	
	
	public LGraphics(Bitmap image){
		if(image==null){
			throw new NullPointerException("Image is null!");
		}
		canvas.setBitmap(image);
		setGraphics(canvas,image.getWidth(),image.getHeight());
	}
	
	public LGraphics(Canvas g,int width,int height){
		setGraphics(g,width,height);
	}
	
	public void setGraphics(Canvas g,int width,int height){
		if(g==null){
			throw new NullPointerException("Graphics is null!");
		}
		this.canvas=g;
		this.width=width;
		this.height=height;
		g.clipRect(0,0,width,height);
	}
	
	/**
	 * 画图像区域
	 * @param image        图像源
     * @param x_src        区域X
     * @param y_src        区域Y
     * @param width        区域WITH
     * @param height       区域HEIGHT
     * @param transform    翻转
     * @param x_dest       绘制位置X 
     * @param y_dest       绘制位置Y
     * @param anchor       描点
	 */
	public final void drawRegion(Image img,int x_src,int y_src,int width,int height,int transform,
			int x_dest,int y_dest,int anchor){
		if(img==null){
			throw new NullPointerException("Image is null!");
		}
		//保存绘制之前的状态
		canvas.save();
		int ix=0,iy=0;
		switch(transform){
			case TRANS_NONE: // 0 
				break;
			case TRANS_ROT90: // 90 
				canvas.rotate(90,x_dest,y_dest);
				iy=height;
				break;
			case TRANS_ROT180: // 180 
				canvas.rotate(180,x_dest,y_dest);
				iy=height;
				ix=width;
				break;
			case TRANS_ROT270: // 270 
				canvas.rotate(270,x_dest,y_dest);
				ix=width;
				break;
			case TRANS_MIRROR: // 镜像
				canvas.scale(-1,1,x_dest,y_dest);
				ix=width;
				break;
			case TRANS_MIRROR_ROT90: // M90 j2me<-->android 270 
				canvas.scale(-1,1,x_dest,y_dest);
				canvas.rotate(270,x_dest,y_dest);
				ix=width;
				iy=height;
				break;
			case TRANS_MIRROR_ROT180: // M180 
				canvas.scale(-1,1,x_dest,y_dest);
				canvas.rotate(180,x_dest,y_dest);
				iy=height;
				break;
			case TRANS_MIRROR_ROT270: // M270 j2me<-->android 90 
				canvas.scale(-1,1,x_dest,y_dest);
				canvas.rotate(90,x_dest,y_dest);
				break;
		}
		int cx=x_dest-ix;
		int cy=y_dest-iy;
		cx-=getAnchorX(anchor,ix);
		cy-=getAnchorY(anchor,iy);
		canvas.clipRect(cx,cy,cx+width,cy+height);
		canvas.drawBitmap(img.getBitmap(),cx-x_src,cy-y_src,paint);
		//重置状态
		canvas.restore();
	}
	
	private static final int getAnchorX(int anchor,int width){
        //right
		int desX=0;
        if((anchor&RIGHT)!=0){
            desX=width;
        }else if((anchor&HCENTER)!=0){
            desX=width/2;
        }
        return desX;
    }

	private static final int getAnchorY(int anchor,int height){
		int desY=0;
        //bottom
        if((anchor&BOTTOM)!=0){
            desY=height;
        }else if((anchor&VCENTER)!=0){
            desY=height/2;
        }
        return desY;
    }
	
	public void creal(){
		canvas.drawColor(0x00000000);
		canvas.clipRect(0,0,width,height);
	}
}
